//
//  DateViewController.m
//  primoPrj
//
//  Created by Giuseppe on 24/01/14.
//
//

#import "DateViewController.h"

@interface DateViewController ()

@end

@implementation DateViewController

@synthesize datePicker;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}



- (id)initWithElement:(Element*)element{
    
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        
        _element = element;
        
        self.view.backgroundColor = [UIColor grayColor];
        
        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 150, 325, 300)];
        datePicker.backgroundColor=[UIColor whiteColor];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.hidden = NO;
        datePicker.date = [NSDate date];
        
        [datePicker addTarget:self
                       action:@selector(selectDate)
             forControlEvents:UIControlEventValueChanged];
        
        
        UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(40,50,60,50)];
        [saveButton setTitle:@"Set" forState:UIControlStateNormal];
        [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveButton.layer setBorderWidth:1.0f];
        [saveButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [saveButton addTarget:self
                       action:@selector(setDate)
             forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(240,50,60,50)];
        [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cancelButton.layer setBorderWidth:1.0f];
        [cancelButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [cancelButton addTarget:self
                         action:@selector(back)
               forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:datePicker];
        [self.view addSubview:saveButton];
        [self.view addSubview:cancelButton];

    }
    return self;
}


-(void) setDate {
    
    NSLog(@"setDate");
    
    //NSDAte to NSString
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *strDate = [dateFormatter stringFromDate: selectedDate];
    _element.date = strDate;
    NSLog(@"in setDate. %@", _element.date);
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) back {
    NSLog(@"back");
    //_element.date = @"nessuna";
    NSLog(@"in back...%@", _element.date);
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)selectDate{
   
    selectedDate = [datePicker date];

}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
